library(ggplot2)
library(readstata13)
library(cregg)
library(tidyverse)

rm(list = ls())

#Read Data In 
data1<-read.dta13("Code/Study3/ConjointReplication/study3_conjointdata_wave1.dta")

colnames(data1)
with(data1, table(id))

#Shrink dataset

data2<-data1 %>% select(c("chosen","ResponseId","id","registration","speech","votemethod","wait","watcher", "pid")) %>% 
  rename("Y_choice"="chosen", "resp_id"="ResponseId", "pid3_lean"="pid")

data2 <- data2 %>% filter(Y_choice !="NA")

data3<-data2 %>%
  mutate(Voter_ID=recode_factor(id,
                                "None Required" = "None",
                                "Signature on Sign in" = "Signature",
                                "Photo ID" = "ID",
                                " Photo ID + Signature on Sign in" = "Sig. and ID"),
         Registration=recode_factor(registration, 
                                    "Registration must be complete 30 days before the election"="30 days prior",
                                    "Registration must be complete 8 days before the election"="8 days prior",
                                    "Same day registration not permitted" = "No same day",
                                    "Same day registration permitted with proper ID" = "Same day with ID",
                                    "Same day registration permitted"="Same day allowed"),
         Speech=recode_factor(speech,
                              "No campaigning allowed within 100 feet"="< 100 ft",
                              "No campaigning allowed within 50 feet"="< 50 ft",
                              "No campaigning allowed within the building"="No campaigning",
                              "No restriction on campaigning"="No restriction"),
         Vote_Method=recode_factor(votemethod,
                                   "Machine (touchscreen), no paper receipt"="Machine/no receipt",
                                   "Machine (touchscreen), prints paper receipt"="Machine/receipt",
                                   "Machine (touchscreen), paper receipt w/ automatic manual audit"="Machine/receipt/automatic audit",
                                   "Machine (touchscreen), machine count w/ manual audit for very close races"="Machine/automatic audit",
                                   "Paper ballot, hand counted"="Paper/hand count",
                                   "Paper ballot, machine counted"="Paper/machine count"),
         Wait_Time=recode_factor(wait,
                                 "<10 minutes"="<10 min.",
                                 "10-30 minutes"="10-30 min.",
                                 "30 minutes-1 hour"="30-60 min.",
                                 "1-3 hours"="1-3 hr.",
                                 "3+hours"="3+ hr."),
         Poll_Watchers=recode_factor(watcher,
                                     "No poll watchers present"="None present",
                                     "Both Republican and Democratic Party poll watchers" = "Both D & R", 
                                     "Both Republican and Democratic Party poll watchers with identification badges" = "Both D & R/Badges",
                                     "Only Democratic Party poll watchers" = "Only D", 
                                     "Only Democratic Party poll watchers with identification badges" = "Only D/Badges", 
                                     "Only Republican Party poll watchers" = "Only R", 
                                     "Only Republican Party poll watchers with identification badges" = "Only R/Badges"))
with(data3, table(Poll_Watchers))
with(data3, table(Wait_Time))
with(data3, table(Vote_Method))
with(data3, table(Speech))
with(data3, table(Registration))
with(data3, table(Voter_ID))
with(data3, table(pid3_lean))

data3<- data3 %>% mutate(PID=recode_factor(pid3_lean,
                                    "1"="Democrat",
                                    "2"="Independent",
                                    "3"="Republican"))

data3<-data3 %>% filter(Poll_Watchers !="NA")

output<-cj(data3, 
     Y_choice~Poll_Watchers+Voter_ID+Registration+Speech+Vote_Method+Wait_Time, id = ~ resp_id)

output2<-output %>% filter(level != "Paper/hand count",  
                             level != "Machine/automatic audit")

output2 %>% filter(feature=="Vote_Method")

x<-plot(output2)
plot_amce_study3<-x+geom_hline(yintercept=c(6,11,16,22,27), color="grey")+
  geom_point(size=3)+
  theme_bw()+
  #theme(legend.title=element_blank(), legend.position="right")+
  ggtitle("Study 3 (Replication)")+
  theme(legend.position = "none")+
  theme(axis.title=element_text(size=14,face="bold"))+
  theme(axis.text=element_text(size=12))+
  theme(strip.text.x = element_text(size = 12))
plot_amce_study3

ggsave(
  "Figures/Appendix/study3_replication_AMCE.tiff",
  device="tiff",
  width=8,
  height=10,
  dpi=300)

#PID Subset

data3_pid<-data3 %>%filter(PID !="NA",  PID !="Independent")
data3_pid$PID

data3_pid<-data3_pid %>% mutate(RepDem=recode_factor(pid3_lean, 
                                              "1"="Dem.",
                                              "3"="Rep."))
with(data3_pid,RepDem)

output<-cj(data3_pid,Y_choice~Poll_Watchers+Voter_ID+Registration+Speech+Vote_Method+Wait_Time,
           id = ~ resp_id,
           by=~RepDem,
           estimate="mm")
output
output2<-output %>% filter(level != "Paper/hand count",  
                           level != "Machine/automatic audit")

output %>% filter(feature=="Vote_Method")
output2 %>% filter(feature=="Vote_Method")

plot_mm_study3<-plot(output2, group="RepDem", size=3)+aes(shape=RepDem,color=RepDem)+
  geom_hline(yintercept=c(6,11,16,22,27), color="grey")+
  scale_shape_manual(values=c(15, 16),na.translate=F) + 
  scale_color_manual(values=c("dark blue","red"), na.translate=F)+
  theme_bw()+
  ggtitle("Study 3 (Replication)")+
  theme(legend.position = "", legend.title=element_blank())+
  theme(axis.title=element_text(size=14,face="bold"))+
  theme(axis.text=element_text(size=12))+
  theme(strip.text.x = element_text(size = 12))
plot_mm_study3

ggsave(
  "Figures/Appendix/study3_replication_marginalmean.tiff",
  device="tiff",
  width=8,
  height=10,
  dpi=300)


